package ganga.custodian.sensors;

import ganga.custodian.CustodianApplication;
import ganga.custodian.human.HumanModel;
import ganga.custodian.human.ModelMonitor;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.IBinder;
import android.widget.Toast;

/**
 * Получает от датчиков значения, передаёт МоделиЧеловека
 * @author Apocrypha
 * @version 0.2
 * @updated 15-июл-2011 21:06:30
 */
public class SensorsMonitor extends Service 
{

	private static final Integer defaultInterval = 900;
	private long lastHeartBeatTimeStamp;
	private boolean isFirstHeartBeatEvent;
	private CustodianApplication application;
	// Display panes for the network and GPS locations, with heading,
	// position, and course data for each.

	private HumanModel humanModel;
	public ModelMonitor m_ModelMonitor;
	private LocationManager locationManager;

	public SensorsMonitor(){
		application = CustodianApplication.getInstance();
	}

	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		lastHeartBeatTimeStamp = 0;
		isFirstHeartBeatEvent = true;
	}
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	/** 
	 * Services are launched on the main Application (or process) thread, 
	 * meaning that any processing done in the
	 * onStartCommand handler will happen on the main GUI thread.
	 * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);
		//Получено оповещение о новом ударе сердца
	
		//Рабор сообщения
		Integer duration = getInterval(intent);
		
		Intent intentForModelMonitor = new Intent("ganga.custodian.human.ModelMonitor");
		intentForModelMonitor.putExtra("RRInervalDuration", duration);
		startService(intentForModelMonitor);
		
		return Service.START_STICKY;
	}
	private void showLocationByToast() {
		String toastMessage;
		Location location = getLocation();
		if (location == null)
			toastMessage = "null";
		else {
			location.getLatitude();
			toastMessage = String.format("Lat: %f", location.getLatitude());
		}
		Toast
			.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT)
			.show();
	}
	
	/**
	 * Calulate interval duration
	 * @param intent
	 * @return
	 */
	private Integer getInterval(Intent intent) {
		Long timeStamp = intent.getLongExtra("TimeStamp", 0L);

		if (isFirstHeartBeatEvent){
			lastHeartBeatTimeStamp = timeStamp;
			isFirstHeartBeatEvent = false;
			return defaultInterval;
		}

		int RRInterval = (int) (timeStamp - lastHeartBeatTimeStamp);
		lastHeartBeatTimeStamp = timeStamp;
		return RRInterval;
	}
	Location getLocation(){
		boolean isOn = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		return null;//TODO netLocation;
	}

}